<?php
include("../db_connect.php");

?>
<script>
$(document).ready(function (){
$("#itemRequest").html(loader).load("admin/itemRequest.php");
$("#brand").change(function () {
		model = $("#src_model").val();
		fname = $("#searchFname").val();
		lname = $("#searchLname").val();	
		brand = $(this).attr("value");
		var model2="model";
		if(brand == "select"){
			brand=="";
		}
		
		$("#src_model").empty().load("profile/selection.php?command="+model2 +"&modelid="+brand);
		$("#itemRequest").html(loader).load("admin/itemRequest.php?brand="+brand+"&model="+model+"&fname="+fname+"&lname="+lname);
		
	});
	
$("#src_model").change(function () {
		var a;
		model = $(this).attr("value");
		a = checkwhite(model);
		model = a;
		fname = $("#searchFname").val();
		lname = $("#searchLname").val();	
		brand = $("#brand").val();
		if(model == ""){
			model=="";
		}
		
		
			$("#itemRequest").html(loader).load("admin/itemRequest.php?brand="+brand+"&model="+model+"&fname="+fname+"&lname="+lname);
			//$("#carsearch").html(loader).load("shop/carsearch_ajax.php?brand="+brand+"&model="+model+"&types="+types+"&conditions="+conditions+"&fuel="+fuel+"&transmission="+transmission);
});

$("#searchFname").keydown(function() {
		model = $("#src_model").val();
		fname = checkwhite($(this).val());
		lname = $("#searchLname").val();	
		brand = $("#brand").val();
		
		if(fname == ""){
			fname =="";
		}

	$("#itemRequest").html(loader).load("admin/itemRequest.php?brand="+brand+"&model="+model+"&fname="+fname+"&lname="+lname);
});
$("#searchLname").keydown(function() {
		model = $("#src_model").val();
		fname = $("#searchFname").val();
		lname = checkwhite($(this).val());	
		brand = $("#brand").val();
		
		if(lname == ""){
			lname =="";
		}
	$("#itemRequest").html(loader).load("admin/itemRequest.php?brand="+brand+"&model="+model+"&fname="+fname+"&lname="+lname);
});
	


$("#requestTo").click(function(){

	var userdata = new Array();
	$("#itemRequest tr td").find("input:checked").each(function(e){
			userdata[e] = $(this).val();
		});
	
		if (userdata.length <=0){
				$("#msgbox").empty().html("no row selected");
				$("#msgbox").dialog({
					modal:true,
					title:"Error",
					width:350,
					height: 200,
					show: "fade",
					hide: "fade",
					buttons: {
						OK: function() {
							$( this ).dialog( "close" );
						}

					},
				});
				
				

		}
		else if(userdata.length >=1){
		var disap = "approve";
		$("#msgbox").empty().html("Display this?");
			$("#msgbox").dialog({
				modal:true,
				width:350,
				height: 200,
				show:"fade",
				hide: "fade",
				buttons: {
					OK: function() {
						$( this ).dialog( "close" );
						approveRequest(userdata, disap);
					},
					CANCEL: function() {
						$( this ).dialog( "close" );
					}
				},

			});
			

		}

});//end of approve.click()
	
$("#requestTon").click(function(){

	var userdata = new Array();
	$(".itemRequest tr td").find("input:checked").each(function(e){
			userdata[e] = $(this).val();
		});
	
		if (userdata.length <=0){
				$("#msgbox").empty().html("no row selected");
				$("#msgbox").dialog({
					modal:true,
					title:"Error",
					width:350,
					height: 200,
					show: "fade",
					hide: "fade",
					buttons: {
						OK: function() {
							$( this ).dialog( "close" );
						}

					},
				});
				
				

		}
		else if(userdata.length >=1){
		var disap = "disapprove";
		$("#msgbox").empty().html("Disapprove?");
			$("#msgbox").dialog({
				modal:true,
				width:350,
				height: 200,
				show:"fade",
				hide: "fade",
				buttons: {
					OK: function() {
						$( this ).dialog( "close" );
						approveRequest(userdata, disap);
					},
					CANCEL: function() {
						$( this ).dialog( "close" );
					}
				},

			});
			

		}

});//end of approve.click()


});
/*approve request user*/
 function approveRequest(itemRequest,app){
	
	if(app =="approve"){
		$.ajax({
			url:"admin/approveitemrequest.php?itemRequest="+itemRequest+"&approve="+app,
			success:function(data){
			
			if(data){
					//$("msgbox").empty().html("");
					$("#msgbox").html("Sucessfully approved");
						$("#msgbox").dialog({
							modal:true,
							title:"",
							width:350,
							height: 200,
							show: "fade",
							hide: "fade",
							buttons: {
								OK: function() {
									$( this ).dialog( "close" );
									location.reload();
									
								}

							},
						});
				}
				else{
					$("#msgbox").empty().html("something went wrong");
						$("#msgbox").dialog({
							modal:true,
							title:"Error",
							width:350,
							height: 200,
							show: "fade",
							hide: "fade",
							buttons: {
								OK: function() {
									$(this).dialog( "close" );
								}

							},
						});
				
				}
			
			
			}


		});

	}else{
	
	$.ajax({
			url:"admin/approveitemrequest.php?itemRequest="+itemRequest+"&approve="+app,
			success:function(data){
			
			
				if(data){
					//$("msgbox").empty().html("");
					$("#msgbox").html("Sucessfully disapproved");
						$("#msgbox").dialog({
							modal:true,
							title:"",
							width:350,
							height: 200,
							show: "fade",
							hide: "fade",
							buttons: {
								OK: function() {
									$( this).dialog( "close" );
									location.reload();
								}

							},
						});
				}
				else{
					$("msgbox").empty().html("something went wrong");
						$("#msgbox").dialog({
							modal:true,
							title:"Error",
							width:350,
							height: 200,
							show: "fade",
							hide: "fade",
							buttons: {
								OK: function() {
									$( this).dialog( "close" );
								}

							},
						});
				
				}
			
			
			}


		});
	
	
	}

}

function delete_item(value){
	if(confirm("Do you want to delete this?")){
	$.ajax({
		
		url:"admin/modifyData.php?category=auto&action=delete&pid="+value,
		success:function(data){
			if(data){
			alert("Successful Delete");
			//$("#itemRequest").html(loader).load("admin/itemRequest.php?brand="+brand+"&model="+model+"&fname="+fname+"&lname="+lname);
			$("#itemRequest").html(loader).load("admin/itemRequest.php");
			}
			else{
				alert("Unsuccessful Delete");
			}
			
		}
	});
	}

}


function checkwhite(model){
	var a = new Array();
	a = model.split(" ");
	var x = a.length;
	
	if(x>1){
		
		return a;
	}
	else{
		
		return model;
	}
	
	
}
</script>

<div id="searchbox">
<!--search div-->
<div>
	<div class="headersearch">
		<label>Posted by:</label>
		<input type="text" id="searchFname" placeholder="First Name"/>
		<label>Last Name:</label>
		<input type="text" id="searchLname"/ placeholder="Last Name">
	</div>
	<div class="headersearch ">
	<label>Brand:</label>
	<select id="brand" value="brand" name="brand"> 				
		<?php 
			include("db_connect.php");
			$sql = mysql_query("SELECT * FROM tblbrand");
			$opt = '<option id="optbrand" name="none" value="all">Select All</option> ';
			while($row = mysql_fetch_array($sql) ){
				$opt .= '<option id="optbrand" name="'.$row["brand"].'" value="'.$row["id"].'" >'.trim($row["brand"]).'</option>';
			}
			echo $opt;
		?>				
	</select>
	<label>Model:</label>
	<select id="src_model" value=""></select>
	</div>
	<div class="headersearch ">
		<label>Action:</label><button id="requestTo" name="display">Display</button> <button id="requestTon" name="out">Out</button>
	</div>
</div>			
		
<!--end search div-->		
</div>

<div id="itemRequest" class="itemRequest2" style="overflow:auto">
	<?php //include("itemRequest.php"); ?>
</div>